package com.mb.mp.controller;

import com.alibaba.fastjson.JSONObject;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import com.mb.common.utils.StringUtils;
import com.mb.mp.domain.HxhBrandDO;
import com.mb.mp.service.IBrandService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 王源
 * @program mp-parent
 * @description 品牌库未完成
 * @date 2018-09-04 16:44
 **/
@Controller
@RequestMapping("/hxh/brand")
public class BrandController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Autowired
    private IBrandService brandService;

    @GetMapping("/brand")
    @RequiresPermissions("mp:brand:brand")
     String brandPage(){return "/mp/brand/brand";}

     /** 
      * @description: 品牌列表
      * @author: wangyuan
      * @date: 2018-09-07 11:50:09
      */
     @ResponseBody
     @GetMapping("/list")
     public PageUtils list(@RequestParam Map<String, Object> params){
         Query query = new Query(params);
         List <HxhBrandDO> list = brandService.list(query);
         Integer count = brandService.count(query);
         PageUtils pageUtils = new PageUtils(list,count);
         return pageUtils;

     }


     /** 
      * @description: 品牌添加保存 
      * @author: wangyuan
      * @date: 2018-09-07 11:50:37
      */
     @ResponseBody
     @PostMapping("/save")
     @RequiresPermissions("mp:brand:add")
     public JSONObject save(@RequestBody HxhBrandDO hxhBrandDO){
         JSONObject resp = new JSONObject();
         String brandName = hxhBrandDO.getBrandName();
         if(StringUtils.isBlank(brandName)){
             resp.put("code",3);
             resp.put("msg","名字不能为空");
             return resp;
         }
         hxhBrandDO.setCreator(getUserId());
         resp =  brandService.insertBrand(hxhBrandDO);
         return resp;

     }


    /** 
     * @description: 品牌修改后保存 
     * @author: wangyuan
     * @date: 2018-09-07 11:51:14
     */
     @ResponseBody
     @PostMapping("/update")
     @RequiresPermissions("mp:brand:edit")
     public JSONObject update(@RequestBody HxhBrandDO hxhBrandDO){
         hxhBrandDO.setUpdater(getUserId());
         hxhBrandDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
         JSONObject resp = brandService.update(hxhBrandDO);
         return resp;
     }


     /** 
      * @description: 品牌删除
      * @author: wangyuan
      * @date: 2018-09-07 11:51:29
      */
     @ResponseBody
     @PostMapping("/delete")
     @RequiresPermissions("mp:brand:delete")
     public R delete(@RequestBody HxhBrandDO hxhBrandDO){
         hxhBrandDO.setDeleter(getUserId());
         hxhBrandDO.setDeleteTime(new Date());
         hxhBrandDO.setDataStatus(0);
         Integer code = brandService.delete(hxhBrandDO);
         if(code==1){
             return R.ok();
         }
        return R.error();
     }
}
